package cn.com.note.view;


import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.support.v7.widget.AppCompatCheckBox;
import android.util.AttributeSet;

import cn.com.note.R;

/**
 * Created by SunFuLin on 2019/3/1.
 */

public class IconFontCheckBox extends AppCompatCheckBox {

    private String checkedTrue;
    private String checkedFalse;
    private boolean checked;

    public IconFontCheckBox(Context context) {
        super(context);
    }

    public IconFontCheckBox(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public IconFontCheckBox(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private void init(Context context, AttributeSet attrs) {
        //挨着给每个Button加入drawable限制边距以控制显示大小
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.IconFontCheckBox);
        checkedTrue = array.getString(R.styleable.IconFontCheckBox_CheckedTrue);
        checkedFalse = array.getString(R.styleable.IconFontCheckBox_CheckedFalse);
        checked= array.getBoolean(R.styleable.IconFontCheckBox_isChecked,false);
        array.recycle();
        setTypeface(Typeface.createFromAsset(context.getAssets(), "iconfont.ttf"));
        setChecked(checked);
    }

    @Override
    public void setChecked(boolean checked) {
        super.setChecked(checked);
        if(checked){
            setText(checkedTrue);
        }else {
            setText(checkedFalse);
        }
    }
}
